<template>
  <div class="q-pa-md">
    <q-table
      :rows="tableData"
      :columns="columns"
      row-key="requestId"
      v-model:pagination="pagination"
      :loading="loading"
      @request="onRequest"
      binary-state-sort
    >
      <template v-slot:top>
        <div class="text-h6">风控财报申请购买流程</div>
        <q-space />
        <q-btn
          color="primary"
          label="刷新"
          @click="refreshData"
        />
      </template>
    </q-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      //分页参数设置
      pagination: {
        sortBy: 'desc',
        descending: false,
        page: 1,
        rowsPerPage: 15,
        rowsNumber: 0
      },
      tableData: [],
      columns: [
        { name: 'requestId', align: 'center', label: '申请ID', field: 'requestId', sortable: true },
        { name: '发起日期', align: 'center', label: '发起日期', field: '发起日期', sortable: true },
        { name: '标题', align: 'center', label: '标题', field: '标题', sortable: true },
        { name: '公司名称', align: 'center', label: '公司名称', field: '公司名称', sortable: true },
        { name: '社会代码证', align: 'center', label: '社会代码证', field: '社会代码证', sortable: true },
        { name: '当前节点', align: 'center', label: '当前节点', field: '当前节点', sortable: true },
      ],
      selectRow: null,
      showEdit: false,
    }
  },
  async mounted() {
    await this.loadData();
  },
  methods: {
    async loadData() {
      this.loading = true;
      try {
        const response = await this.$API.riskbrain.GetDataPagingNew.get(this.pagination.page, this.pagination.rowsPerPage);
        console.log('API响应:', response);

        // 正确处理API返回的数据结构
        if (response && response.data) {
          this.tableData = response.data;
          this.pagination.rowsNumber = response.total || 0;
          this.pagination.page = response.page || 1;
          this.pagination.rowsPerPage = response.pageSize || 15;
        } else {
          console.error('API返回数据格式不正确:', response);
          this.tableData = [];
          this.pagination.rowsNumber = 0;
        }
      } catch (error) {
        console.error('加载数据失败:', error);
        this.$q.notify({
          type: 'negative',
          message: '加载数据失败: ' + error.message
        });
      } finally {
        this.loading = false;
      }
    },

    async onRequest(props) {
      const { page, rowsPerPage, sortBy, descending } = props.pagination;

      this.pagination.page = page;
      this.pagination.rowsPerPage = rowsPerPage;
      this.pagination.sortBy = sortBy;
      this.pagination.descending = descending;

      await this.loadData();
    },

    async refreshData() {
      this.pagination.page = 1;
      await this.loadData();
    }
  },
}
</script>

<style>
</style>